/**
# Copyright (C) 2024 HiHope Open Source Organization .
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 */

#include "securec.h"
#include "sle_common.h"
#include "osal_debug.h"
#include "sle_errcode.h"
#include "osal_addr.h"
#include "osal_task.h"
#include "sle_connection_manager.h"
#include "sle_device_discovery.h"
#include "sle_uart_server_adv.h"
#include "sle_uart_server.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "ohos_sle_common.h"
#include "ohos_sle_errcode.h"
#include "ohos_sle_ssap_server.h"
#include "ohos_sle_ssap_client.h"
#include "ohos_sle_device_discovery.h"
#include "ohos_sle_connection_manager.h"
#include "iot_uart.h"
#include "pinctrl.h"
#include "uart.h"
#include "errcode.h"
#include "usr_spi_connection.h"
#include "fs_operation.h"

#define OCTET_BIT_LEN 8
#define UUID_LEN_2 2
#define UUID_INDEX 14
#define BT_INDEX_4     4
#define BT_INDEX_0     0
#define UART_BUFF_LENGTH    0x100

/* 广播ID */
#define SLE_ADV_HANDLE_DEFAULT                    1
/* sle server app uuid for test */
static char g_sle_uuid_app_uuid[UUID_LEN_2] = { 0x12, 0x34 };
/* server notify property uuid for test */
static char g_sle_property_value[OCTET_BIT_LEN] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
/* sle connect acb handle */
static uint16_t g_sle_conn_hdl = 0;
/* sle server handle */
static uint8_t g_server_id = 0;
/* sle service handle */
static uint16_t g_service_handle = 0;
/* sle ntf property handle */
static uint16_t g_property_handle = 0;
/* sle pair acb handle */
uint16_t g_sle_pair_hdl;

#define UUID_16BIT_LEN 2
#define UUID_128BIT_LEN 16
#define sample_at_log_print(fmt, args...) osal_printk(fmt, ##args)
#define SLE_UART_SERVER_LOG "[sle uart server]"
#define SLE_SERVER_INIT_DELAY_MS    1000
static sle_uart_server_msg_queue g_sle_uart_server_msg_queue = NULL;
static uint8_t g_sle_uart_base[] = { 0x37, 0xBE, 0xA8, 0x80, 0xFC, 0x70, 0x11, 0xEA, \
    0xB7, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static uint8_t g_at_pre_char = 0;
static uint32_t g_at_uart_recv_cnt = 0;

#define SLE_UART_TRANSFER_SIZE              256
static uint8_t g_app_uart_rx_buff[SLE_UART_TRANSFER_SIZE] = { 0 };

static uart_buffer_config_t g_app_uart_buffer_config = {
    .rx_buffer = g_app_uart_rx_buff,
    .rx_buffer_size = SLE_UART_TRANSFER_SIZE
};

static void server_uart_rx_callback(const void *buffer, uint16_t length, bool error)
{
    errcode_t ret;
    
    if(length > 0){
        ret = uart_sle_send_data((uint8_t *)buffer,(uint8_t)length);
        if(ret != 0){
     osal_printk("\r\nsle_server_send_data_fail:%d\r\n", ret);
   }
}

}

static void uart_init_config(void)
{
    uart_attr_t attr = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_BIT_8,
        .stop_bits = UART_STOP_BIT_1,
        .parity = UART_PARITY_NONE
    };

    uart_pin_config_t pin_config = {
        .tx_pin = 0,
        .rx_pin = 0,
        .cts_pin = PIN_NONE,
        .rts_pin = PIN_NONE
    };
    uapi_uart_deinit(0);
    uapi_uart_init(0, &pin_config, &attr, NULL, &g_app_uart_buffer_config);
      (void)uapi_uart_register_rx_callback(0, UART_RX_CONDITION_FULL_OR_IDLE,
                                         1, server_uart_rx_callback);
}




static void encode2byte_little(uint8_t *_ptr, uint16_t data)
{
    *(uint8_t *)((_ptr) + 1) = (uint8_t)((data) >> 0x8);
    *(uint8_t *)(_ptr) = (uint8_t)(data);
}

static void sle_uuid_set_base(SleUuid *out)
{
    errcode_t ret;
    ret = memcpy_s(out->uuid, SLE_UUID_LEN, g_sle_uart_base, SLE_UUID_LEN);
    if (ret != EOK) {
        sample_at_log_print("%s sle_uuid_set_base memcpy fail\n", SLE_UART_SERVER_LOG);
        out->len = 0;
        return ;
    }
    out->len = UUID_LEN_2;
}

static void sle_uuid_setu2(uint16_t u2, SleUuid *out)
{
    sle_uuid_set_base(out);
    out->len = UUID_LEN_2;
    encode2byte_little(&out->uuid[UUID_INDEX], u2);
}
static void sle_uart_uuid_print(SleUuid *uuid)
{
    if (uuid == NULL) {
        sample_at_log_print("%s uuid_print,uuid is null\r\n", SLE_UART_SERVER_LOG);
        return;
    }
    if (uuid->len == UUID_16BIT_LEN) {
        sample_at_log_print("%s uuid: %02x %02x.\n", SLE_UART_SERVER_LOG,
            uuid->uuid[14], uuid->uuid[15]); /* 14 15: uuid index */
    } else if (uuid->len == UUID_128BIT_LEN) {
        sample_at_log_print("%s uuid: \n", SLE_UART_SERVER_LOG); /* 14 15: uuid index */
        sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[0], uuid->uuid[1],
            uuid->uuid[2], uuid->uuid[3]);
        sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[4], uuid->uuid[5],
            uuid->uuid[6], uuid->uuid[7]);
        sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[8], uuid->uuid[9],
            uuid->uuid[10], uuid->uuid[11]);
        sample_at_log_print("%s 0x%02x 0x%02x 0x%02x \n", SLE_UART_SERVER_LOG, uuid->uuid[12], uuid->uuid[13],
            uuid->uuid[14], uuid->uuid[15]);
    }
}

static void ssaps_mtu_changed_cbk(uint8_t server_id, uint16_t conn_id,  SsapcExchangeInfo *mtu_size,
    errcode_t status)
{
    sample_at_log_print("%s ssaps ssaps_mtu_changed_cbk callback server_id:%x, conn_id:%x, mtu_size:%x, status:%x\r\n",
        SLE_UART_SERVER_LOG, server_id, conn_id, mtu_size->mtuSize, status);
    if (g_sle_pair_hdl == 0) {
        g_sle_pair_hdl = conn_id + 1;
    }
}

static void ssaps_start_service_cbk(uint8_t server_id, uint16_t handle, errcode_t status)
{
    sample_at_log_print("%s start service cbk callback server_id:%d, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG,
        server_id, handle, status);
}
static void ssaps_add_service_cbk(uint8_t server_id, SleUuid *uuid, uint16_t handle, errcode_t status)
{
    sample_at_log_print("%s add service cbk callback server_id:%x, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG,
        server_id, handle, status);
    sle_uart_uuid_print(uuid);
}
static void ssaps_add_property_cbk(uint8_t server_id, SleUuid *uuid, uint16_t service_handle,
    uint16_t handle, errcode_t status)
{
    sample_at_log_print("%s add property cbk callback server_id:%x, service_handle:%x,handle:%x, status:%x\r\n",
        SLE_UART_SERVER_LOG, server_id, service_handle, handle, status);
    sle_uart_uuid_print(uuid);
}
static void ssaps_add_descriptor_cbk(uint8_t server_id, SleUuid *uuid, uint16_t service_handle,
    uint16_t property_handle, errcode_t status)
{
    sample_at_log_print("%s add descriptor cbk callback server_id:%x, service_handle:%x, property_handle:%x, \
        status:%x\r\n", SLE_UART_SERVER_LOG, server_id, service_handle, property_handle, status);
    sle_uart_uuid_print(uuid);
}
static void ssaps_delete_all_service_cbk(uint8_t server_id, errcode_t status)
{
    sample_at_log_print("%s delete all service callback server_id:%x, status:%x\r\n", SLE_UART_SERVER_LOG,
        server_id, status);
}
static errcode_t sle_ssaps_register_cbks(SsapsReadRequestCallback ssaps_read_callback, SsapsWriteRequestCallback
    ssaps_write_callback)
{
    errcode_t ret;
    SsapsCallbacks ssaps_cbk = {0};
    ssaps_cbk.addServiceCb = ssaps_add_service_cbk;
    ssaps_cbk.addPropertyCb = ssaps_add_property_cbk;
    ssaps_cbk.addDescriptorCb = ssaps_add_descriptor_cbk;
    ssaps_cbk.startServiceCb = ssaps_start_service_cbk;
    ssaps_cbk.deleteAllServiceCb = ssaps_delete_all_service_cbk;
    ssaps_cbk.mtuChangedCb = ssaps_mtu_changed_cbk;
    ssaps_cbk.readRequestCb = ssaps_read_callback;
    ssaps_cbk.writeRequestCb = ssaps_write_callback;
    ret = SsapsRegisterCallbacks(&ssaps_cbk);
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_ssaps_register_cbks,ssaps_register_callbacks fail :%x\r\n", SLE_UART_SERVER_LOG,
            ret);
        return ret;
    }
    return ERRCODE_SLE_SUCCESS;
}

static errcode_t sle_uuid_server_service_add(void)
{
    errcode_t ret;
    SleUuid service_uuid = {0};
    sle_uuid_setu2(SLE_UUID_SERVER_SERVICE, &service_uuid);
    ret = SsapsAddServiceSync(g_server_id, &service_uuid, 1, &g_service_handle);
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle uuid add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret);
        return ERRCODE_SLE_FAIL;
    }
    return ERRCODE_SLE_SUCCESS;
}

static errcode_t sle_uuid_server_property_add(void)
{
    errcode_t ret;
    SsapsPropertyInfo property = {0};
    SsapsDescInfo descriptor = {0};
    uint8_t ntf_value[] = { 0x01, 0x02 };

    property.permissions = SLE_UUID_TEST_PROPERTIES;
    property.operateIndication = SLE_UUID_TEST_OPERATION_INDICATION;
    sle_uuid_setu2(SLE_UUID_SERVER_NTF_REPORT, &property.uuid);
    property.valueLen = OCTET_BIT_LEN;
    property.value = (uint8_t *)osal_vmalloc(sizeof(g_sle_property_value));
    
    if (property.value == NULL) {
        return ERRCODE_SLE_FAIL;
    }
    if (memcpy_s(property.value, sizeof(g_sle_property_value), g_sle_property_value,
        sizeof(g_sle_property_value)) != EOK) {
        osal_vfree(property.value);
        return ERRCODE_SLE_FAIL;
    }
    ret = SsapsAddPropertySync(g_server_id, g_service_handle, &property,  &g_property_handle);
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle uart add property fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret);
        osal_vfree(property.value);
        return ERRCODE_SLE_FAIL;
    }
    descriptor.permissions = SLE_UUID_TEST_DESCRIPTOR;
    descriptor.type = SSAP_DESCRIPTOR_CLIENT_CONFIGURATION;
    descriptor.operateIndication = SLE_UUID_TEST_OPERATION_INDICATION;
    descriptor.value = (uint8_t *)osal_vmalloc(sizeof(ntf_value));
    if (descriptor.value == NULL) {
        osal_vfree(property.value);
        return ERRCODE_SLE_FAIL;
    }
    if (memcpy_s(descriptor.value, sizeof(ntf_value), ntf_value, sizeof(ntf_value)) != EOK) {
        osal_vfree(property.value);
        osal_vfree(descriptor.value);
        return ERRCODE_SLE_FAIL;
    }
    ret = SsapsAddDescriptorSync(g_server_id, g_service_handle, g_property_handle, &descriptor);
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle uart add descriptor fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret);
        osal_vfree(property.value);
        osal_vfree(descriptor.value);
        return ERRCODE_SLE_FAIL;
    }
    osal_vfree(property.value);
    osal_vfree(descriptor.value);
    return ERRCODE_SLE_SUCCESS;
}

static errcode_t sle_uart_server_add(void)
{
    errcode_t ret;
    sle_uuid_t app_uuid = {0};

    sample_at_log_print("%s sle uart add service in\r\n", SLE_UART_SERVER_LOG);
    app_uuid.len = sizeof(g_sle_uuid_app_uuid);
    if (memcpy_s(app_uuid.uuid, app_uuid.len, g_sle_uuid_app_uuid, sizeof(g_sle_uuid_app_uuid)) != EOK) {
        return ERRCODE_SLE_FAIL;
    }
    ssapsRegisterServer(&app_uuid, &g_server_id);

    if (sle_uuid_server_service_add() != ERRCODE_SLE_SUCCESS) {
       SsapsUnregisterServer(g_server_id);
        return ERRCODE_SLE_FAIL;
    }
    if (sle_uuid_server_property_add() != ERRCODE_SLE_SUCCESS) {
        SsapsUnregisterServer(g_server_id);
        return ERRCODE_SLE_FAIL;
    }
    sample_at_log_print("%s sle uart add service, server_id:%x, service_handle:%x, property_handle:%x\r\n",
        SLE_UART_SERVER_LOG, g_server_id, g_service_handle, g_property_handle);
    ret = SsapsStartService(g_server_id, g_service_handle);
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle uart add service fail, ret:%x\r\n", SLE_UART_SERVER_LOG, ret);
        return ERRCODE_SLE_FAIL;
    }
    sample_at_log_print("%s sle uart add service out\r\n", SLE_UART_SERVER_LOG);
    return ERRCODE_SLE_SUCCESS;
}

/* device通过handle向host发送数据：report */
errcode_t sle_uart_server_send_report_by_handle(const uint8_t *data, uint8_t len)
{
    SsapsNtfInd param = {0};
    uint8_t receive_buf[UART_BUFF_LENGTH] = { 0 }; /* max receive length. */
    param.handle = g_property_handle;
    param.type = SSAP_PROPERTY_TYPE_VALUE;
    param.value = receive_buf;
    param.valueLen = len+1;
    if (memcpy_s(param.value, param.valueLen, data, len) != EOK) {
        return ERRCODE_SLE_FAIL;
    }
    return  SsapsNotifyIndicate(g_server_id, g_sle_conn_hdl, &param);
}

static void sle_connect_state_changed_cbk(uint16_t conn_id, const SleAddr *addr,
    SleAcbStateType conn_state, SlePairStateType pair_state, SleDiscReasonType disc_reason)
{
    uint8_t sle_connect_state[] = "sle_dis_connect";
    sample_at_log_print("%s connect state changed callback conn_id:0x%02x, conn_state:0x%x, pair_state:0x%x, \
        disc_reason:0x%x\r\n", SLE_UART_SERVER_LOG,conn_id, conn_state, pair_state, disc_reason);
    sample_at_log_print("%s connect state changed callback addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG,
        addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]);
    if (conn_state == OH_SLE_ACB_STATE_CONNECTED) {
        g_sle_conn_hdl = conn_id;
      
    } else if (conn_state == OH_SLE_ACB_STATE_DISCONNECTED) {
        g_sle_conn_hdl = 0;
        g_sle_pair_hdl = 0;
        if (g_sle_uart_server_msg_queue != NULL) {
            g_sle_uart_server_msg_queue(sle_connect_state, sizeof(sle_connect_state));
        }
    }
}

static void sle_pair_complete_cbk(uint16_t conn_id, const SleAddr *addr, errcode_t status)
{
    sample_at_log_print("%s pair complete conn_id:%02x, status:%x\r\n", SLE_UART_SERVER_LOG,
        conn_id, status);
    sample_at_log_print("%s pair complete addr:%02x:**:**:**:%02x:%02x\r\n", SLE_UART_SERVER_LOG,
        addr->addr[BT_INDEX_0], addr->addr[BT_INDEX_4]);
    g_sle_pair_hdl = conn_id + 1;

}

static errcode_t sle_conn_register_cbks(void)
{
    errcode_t ret;
    SleConnectionCallbacks conn_cbks = {0};
    conn_cbks.connectStateChangedCb = sle_connect_state_changed_cbk;
    conn_cbks.pairCompleteCb = sle_pair_complete_cbk;
    ret = SleConnectionRegisterCallbacks(&conn_cbks);
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_conn_register_cbks,sle_connection_register_callbacks fail :%x\r\n",
        SLE_UART_SERVER_LOG, ret);
        return ret;
    }
    return ERRCODE_SLE_SUCCESS;
}



void ssaps_read_request_callbacks(uint8_t server_id, uint16_t conn_id, ssaps_req_read_cb_t *read_cb_para,
    errcode_t status){
        (void)server_id;
        (void)conn_id;
        (void)read_cb_para;
        (void)status;

    }

void ssaps_write_request_callbacks(uint8_t server_id, uint16_t conn_id, ssaps_req_write_cb_t *write_cb_para,
    int status) {
    (void)server_id;
    (void)conn_id;
    (void)status;

    if (write_cb_para->length <= 0) {
        printf("无效的写入长度\n");
        return;
    }

    write_cb_para->value[write_cb_para->length - 1] = '\0'; // 确保字符串以 '\0' 结尾

    printf("client_send_data: %s\r\n", write_cb_para->value);

// 收到指令后进行解析
    char *p = (char *)write_cb_para->value;
    char *argv[3];
    int argc = 0;

    argv[0] = p;
    char *space1 = strchr(p, ' ');
    if (space1 != NULL) {
        *space1 = '\0';
        argv[1] = space1 + 1;
        argc = 2;
        char *space2 = strchr(argv[1], ' ');
        if (space2 != NULL) {
            *space2 = '\0';
            argv[2] = space2 + 1;
            argc = 3;
        }
    } else {
        argc = 1;
    }

// 打印调试信息
    printf("[nl_debug]Received command: %s %s\n", argv[0], argc > 1 ? argv[1] : "");
    printf("argc: %d\n", argc);
    printf("argv[0]: %s\n", argv[0]);
    printf("argv[1]: %s\n", argc > 1 ? argv[1] : "");
    printf("argv[2]: %s\n", argc > 2 ? argv[2] : "");
    // 根据指令执行对应操作
    if (strcmp(argv[0], "ls") == 0) {
        int count = 0;
        char *path = (argc > 1) ? argv[1] : "/"; // 默认使用根目录
        FileNode **file_list = command_ls(path, &count); // 修改后的 command_ls 返回 FileNode**
        
        if (file_list == NULL) {
            if (count == 0) {
                printf("目录为空或不存在：%s\n", path);
                // 发送 JSON 消息
                char error_json[256];
                snprintf(error_json, sizeof(error_json), "[nl_device]{\"status\":200, \"message\":\"目录为空或不存在：%s\"}", path);
                uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
            } else {
                printf("内存分配失败\n");
                // 发送 JSON 错误消息
                char error_json[] = "[nl_device]{\"status\":500, \"message\":\"内存分配失败\"}";
                uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
            }
            return;
        }

        // 构建 JSON 格式的文件列表
        // 每个目录前加 '#'，文件前加 '@'
        // 使用动态构建，避免缓冲区溢出
        size_t json_size = 256;
        char *json_str = (char *)malloc(json_size);
        if (json_str == NULL) {
            printf("内存分配失败\n");
            free(file_list);
            char error_json[] = "[nl_device]{\"status\":500, \"message\":\"内存分配失败\"}";
            uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
            return;
        }

        strcpy(json_str, "[nl_device]{\"status\":200, \"files\":[");

        for (int i = 0; i < count; i++) {
            char entry[300];
            if (file_list[i]->type == FILE_TYPE_DIRECTORY) {
                snprintf(entry, sizeof(entry), "\"#%s\"", file_list[i]->name);
            } else if (file_list[i]->type == FILE_TYPE_FILE) {
                snprintf(entry, sizeof(entry), "\"@%s\"", file_list[i]->name);
            } else {
                snprintf(entry, sizeof(entry), "\"%s\"", file_list[i]->name);
            }

            // 动态扩展 JSON 字符串
            if (strlen(json_str) + strlen(entry) + 3 > json_size) {
                json_size *= 2;
                json_str = (char *)realloc(json_str, json_size);
                if (json_str == NULL) {
                    printf("内存分配失败\n");
                    free(file_list);
                    char error_json[] = "[nl_device]{\"status\":500, \"message\":\"内存分配失败\"}";
                    uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
                    return;
                }
            }

            strcat(json_str, entry);
            if (i < count - 1) {
                strcat(json_str, ", ");
            }
        }
        strcat(json_str, "]}");

        // 打印调试信息
        printf("目录 %s 下的文件和子目录：\n", path);
        for (int i = 0; i < count; i++) {
            printf("%s\t", file_list[i]->name);
            // 打印类型信息
            if (file_list[i]->type == FILE_TYPE_DIRECTORY) {
                printf("(Directory)\t");
            } else if (file_list[i]->type == FILE_TYPE_FILE) {
                printf("(File)\t");
            } else {
                printf("(Unknown)\t");
            }
        }
        printf("\n");
        printf("JSON 输出: %s\n", json_str); // 调试信息

        // 发送 JSON 字符串
        uart_sle_send_data((uint8_t *)json_str, strlen(json_str));

        // 释放分配的内存
        free(json_str);
        free(file_list);
        
    } else if (strcmp(argv[0], "rm") == 0) {
        if (argc > 1) {
            command_rm(argv[1]);
            // 发送 JSON 成功消息
            char success_json[256];
            snprintf(success_json, sizeof(success_json), "[nl_device]{\"status\":200, \"message\":\"删除成功：%s\"}", argv[1]);
            uart_sle_send_data((uint8_t *)success_json, strlen(success_json));
        } else {
            printf("请提供要删除的文件或目录路径。\n");
            // 发送 JSON 错误消息
            char error_json[] = "[nl_device]{\"status\":400, \"message\":\"请提供要删除的文件或目录路径。\"}";
            uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
        }
    } else if (strcmp(argv[0], "touch") == 0) {
        if (argc > 1) {
            command_touch(argv[1]);
            // 发送 JSON 成功消息
            char success_json[256];
            snprintf(success_json, sizeof(success_json), "[nl_device]{\"status\":200, \"message\":\"创建文件成功：%s\"}", argv[1]);
            uart_sle_send_data((uint8_t *)success_json, strlen(success_json));
        } else {
            printf("请提供要创建的文件路径。\n");
            // 发送 JSON 错误消息
            char error_json[] = "[nl_device]{\"status\":400, \"message\":\"请提供要创建的文件路径。\"}";
            uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
        }
    } else if (strcmp(argv[0], "mkdir") == 0) {
        if (argc > 1) {
            command_mkdir(argv[1]);
            // 发送 JSON 成功消息
            char success_json[256];
            snprintf(success_json, sizeof(success_json), "[nl_device]{\"status\":200, \"message\":\"创建目录成功：%s\"}", argv[1]);
            uart_sle_send_data((uint8_t *)success_json, strlen(success_json));
        } else {
            printf("请提供要创建的目录路径。\n");
            // 发送 JSON 错误消息
            char error_json[] = "[nl_device]{\"status\":400, \"message\":\"请提供要创建的目录路径。\"}";
            uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
        }
    } else if(strcmp(argv[0], "cat") == 0) {
        if (argc > 1) {
            char *data = command_cat(argv[1]);
            if (data != NULL) {
                // 发送 JSON 成功消息
                char success_json[256];
                snprintf(success_json, sizeof(success_json), "[nl_device]{\"status\":200, \"message\":\"读取文件成功：%s\", \"data\":\"%s\"}", argv[1], data);
                uart_sle_send_data((uint8_t *)success_json, strlen(success_json));
            } else {
                // 发送 JSON 错误消息
                char error_json[256];
                snprintf(error_json, sizeof(error_json), "[nl_device]{\"status\":400, \"message\":\"读取文件失败：%s\"}", argv[1]);
                uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
            }
        } else {
            printf("请提供要读取的文件路径。\n");
            // 发送 JSON 错误消息
            char error_json[] = "[nl_device]{\"status\":400, \"message\":\"请提供要读取的文件路径。\"}";
            uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
        }
    }
    else if(strcmp(argv[0], "write") == 0) {
        if (argc > 2) {
            command_write(argv[1], argv[2]);
            // 发送 JSON 成功消息
            char success_json[256];
            snprintf(success_json, sizeof(success_json), "[nl_device]{\"status\":200, \"message\":\"写入文件成功：%s\"}", argv[1]);
            uart_sle_send_data((uint8_t *)success_json, strlen(success_json));
        } else {
            printf("请提供要写入的文件路径和数据。\n");
            // 发送 JSON 错误消息
            char error_json[] = "[nl_device]{\"status\":400, \"message\":\"请提供要写入的文件路径和数据。\"}";
            uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
        }
    }
    else{
        printf("未知指令：%s\n", argv[0]);
        // 发送 JSON 错误消息
        char error_json[256];
        snprintf(error_json, sizeof(error_json), "[nl_device]{\"status\":400, \"message\":\"未知指令：%s\"}", argv[0]);
        uart_sle_send_data((uint8_t *)error_json, strlen(error_json));
    }
}
/* 初始化uuid server */
errcode_t sle_uart_server_init()
{
    errcode_t ret;
    ret = sle_uart_announce_register_cbks();
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_uart_server_init,sle_uart_announce_register_cbks fail :%x\r\n",
        SLE_UART_SERVER_LOG, ret);
        return ret;
    }
    ret = sle_conn_register_cbks();
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_uart_server_init,sle_conn_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret);
        return ret;
    }
    ret = sle_ssaps_register_cbks(ssaps_read_request_callbacks, ssaps_write_request_callbacks);
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_uart_server_init,sle_ssaps_register_cbks fail :%x\r\n", SLE_UART_SERVER_LOG, ret);
        return ret;
    }
    ret = EnableSle();
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_uart_server_init,enable_sle fail :%x\r\n", SLE_UART_SERVER_LOG, ret);
        return ret;
    }
    sample_at_log_print("%s init ok\r\n", SLE_UART_SERVER_LOG);
    return ERRCODE_SLE_SUCCESS;
}

errcode_t sle_enable_server_cbk(void)
{
    errcode_t ret;
    ret = sle_uart_server_add();
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_uart_server_init,sle_uart_server_add fail :%x\r\n", SLE_UART_SERVER_LOG, ret);
        return ret;
    }
    ret = sle_uart_server_adv_init();
    if (ret != ERRCODE_SLE_SUCCESS) {
        sample_at_log_print("%s sle_uart_server_init,sle_uart_server_adv_init fail :%x\r\n", SLE_UART_SERVER_LOG, ret);
        return ret;
    }
    return ERRCODE_SLE_SUCCESS;
}



void sle_uart_server_register_msg(sle_uart_server_msg_queue sle_uart_server_msg)
{
    g_sle_uart_server_msg_queue = sle_uart_server_msg;
}


int uart_sle_send_data(uint8_t *data,uint8_t length){
    int ret;
    ret = sle_uart_server_send_report_by_handle(data,length);
    return ret ;
}


static void SleTask( char* arg)
{
    (void)arg;
    usleep(1000000);
    uart_init_config();
    spi_init();
    init_file_system(); // 初始化文件系统
    sle_uart_server_init();
    return NULL;
}

static void SleServerExample(void)
{
    osThreadAttr_t attr;

    attr.name = "SleTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 4096;
    attr.priority = 25;

    if (osThreadNew(SleTask, NULL, &attr) == NULL) {
       printf("[SleExample] Falied to create SleTask!\n");
    }else printf("[SleExample]  create SleTask successfully !\n");
}

SYS_RUN(SleServerExample); // if test add it